<?php

session_start();
error_reporting (E_ALL ^ E_NOTICE); 

include('../includes/smarty.php');
include_once('../config.php');

if(empty($_POST)) 
{
	// register the new form.
	SmartyValidate::connect($smarty, true);
	
    SmartyValidate::register_validator('rv_companyName',	'companyName',			'notEmpty',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_address',		'address',				'notEmpty',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_city',		 	'city',					'notEmpty',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_zip',		 	'zip',					'isNumber',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_address2',	 	'address2',				'notEmpty',	true, 	false, 'trim');
	SmartyValidate::register_validator('rv_city2',		 	'city2',				'notEmpty',	true, 	false, 'trim');
	SmartyValidate::register_validator('rv_zip2',		 	'zip2',					'isNumber',	true, 	false, 'trim');
    SmartyValidate::register_validator('rv_email', 			'email', 				'isEmail', 	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_username',		'username',				'notEmpty',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_password',		'password',				'notEmpty',	false, 	false, 'trim');
	SmartyValidate::register_validator('rv_password2',		'password:password2',	'isEqual',	false, 	false, 'trim');
	
    // display form
    $smarty->assign('title', 	'Company Information');
	$smarty->assign('path', 	$site);
	$smarty->assign('leftCol', 	'main/newsFeed.tpl');
	$smarty->assign('mainCol', 	'main/posterSignup.tpl');
	$smarty->assign('rightCol', 'main/signin.tpl');
	$smarty->assign('states', 	get_state_select());
	$smarty->assign('industry', get_industry_select());
	$smarty->assign('zipREX', 	$regex_zip);
	$smarty->assign('phoneREX', $regex_phone);
	$smarty->assign('emailREX', $regex_email);

	$smarty->display('index.tpl');
}
else 
{    
	SmartyValidate::connect($smarty);
    if(SmartyValidate::is_valid($_POST)) 
	{
		// this post is valid.
		
		$compName	= $_POST["companyName"];	//posts companyname
		$industry 	= $_POST["industry"];		//posts industry
		$address 	= $_POST["address"];		//posts address
		$city 		= $_POST["city"];			//posts city
		$state 		= $_POST["state"];			//posts state
		$zipcode 	= $_POST["zip"];			//posts zipcode
		$address2	= $_POST["address2"];		//posts address
		$city2	 	= $_POST["city2"];			//posts city
		$state2 	= $_POST["state2"];			//posts state
		$zipcode2	= $_POST["zip2"];			//posts zipcode
		$phone 		= $_POST["phone"];			//posts phone
		$fax 		= $_POST["fax"];			//posts fax
		$website 	= $_POST["website"];		//posts website
		$email 		= $_POST["email"];			//posts email
		$username	= $_POST["username"];		//posts username
		$password	= $_POST["password"];		//posts password
		$password2	= $_POST["password2"];		//posts password
		
		//	store entries in database.
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = "insert into wms_employer ";
		$sql .= "(company_name, industry, address, city, state, zip, second_address, second_city, second_state, second_zip, phone_number, fax_number, website, email_address, date_register, username, password) ";
		$sql .= "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');";
		
		$sql = sprintf($sql 
			,mysql_real_escape_string($compName)
			,mysql_real_escape_string($industry)
			,mysql_real_escape_string($address)
			,mysql_real_escape_string($city)
			,mysql_real_escape_string($state)
			,mysql_real_escape_string($zipcode)
			,mysql_real_escape_string($address2)
			,mysql_real_escape_string($city2)
			,mysql_real_escape_string($state2)
			,mysql_real_escape_string($zipcode2)
			,mysql_real_escape_string($phone)
			,mysql_real_escape_string($fax)
			,mysql_real_escape_string($website)
			,mysql_real_escape_string($email)
			,mysql_real_escape_string(date('Y-m-d'))
			,mysql_real_escape_string($username)
			,mysql_real_escape_string(hash(algor, $password)));
		
		$result = mysql_query($sql);
		
		if(!$result) die("An error has occured: " . mysql_error());
		mysql_close($conn);
		
		//	success
		header('Location: success.php');
	}
	   else
	   {
			$smarty->assign('title', 	'Company Information');
			$smarty->assign('path', 	$site);
			$smarty->assign('leftCol', 	'main/newsFeed.tpl');
			$smarty->assign('mainCol', 	'main/posterSignup.tpl');
			$smarty->assign('rightCol', 'main/signin.tpl');
			$smarty->assign('states', 	get_state_select());
			$smarty->assign('industry', get_industry_select());
			$smarty->assign('zipREX', 	$regex_zip);
			$smarty->assign('phoneREX', $regex_phone);
			$smarty->assign('emailREX', $regex_email);

			$smarty->display('index.tpl');
	   }
}

?>